package com.xiaoyu.lanling.feature.serverpush.event

import `in`.srain.cube.concurrent.AppThreads
import `in`.srain.cube.request.JsonData
import com.xiaoyu.base.serverpush.event.ServerPushBaseEvent
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.base.utils.extensions.convertAsList
import com.xiaoyu.im.IMLog.logNetError
import com.xiaoyu.im.data.ConversationData
import com.xiaoyu.im.provider.ConversationManager
import io.reactivex.functions.Function

class ConversationListUpdatePush(val jsonData: JsonData) : ServerPushBaseEvent(jsonData) {
    private val chatIdList: List<String> = jsonData.optJson("chatIdList").convertAsList(Function<Int, String> { t -> t.toString() })

    override fun takeAction() {
        AppThreads.runOnIOThread {
            val filteredChatIdList = ListUtil.filterList(chatIdList) { s -> ConversationManager.getInstance().isConversationInLocal(s) }
            if (filteredChatIdList.isNotEmpty()) {
                val success = ConversationData.syncConversationListFromServer(filteredChatIdList)
                if (!success) {
                    val desc = String.format("update conversation, jsonData=%s", jsonData)
                    logNetError("fetch_conv_by_chat_id", desc)
                }
            }
            setComplete()
        }
    }

}